package com.didibigdata.servlets;

import com.didibigdata.Repositories.PointOutRepository;
import com.didibigdata.entities.PointOutOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/PointOutServletExchange")
public class PointOutServletExchange extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //日志
        Logger logger = LoggerFactory.getLogger(PointOutServletExchange.class);
        logger.info("add PointOutServletExchange logger successful!");

        String dayName = new String();
        String inOrOut = new String();
        request.setCharacterEncoding("utf-8");
        try {
            dayName = request.getParameter("dayName");
            inOrOut = request.getParameter("inOrOut");
            logger.info("dayName: [{}] and inOrOut: [{}]", dayName, inOrOut);
        } catch (Exception e) {
            logger.error("PointOutServletExchange request get fail!");
            e.printStackTrace();
        }

        String sqltimeTest = new String();
        if (dayName.isEmpty()) {
            sqltimeTest = "select * from t_didi201610_train_out_200m_day1_2to4 order by timestp";
        } else {
            sqltimeTest = "select * from t_didi201610_train_" + inOrOut + dayName + " order by timestp";
        }

        PointOutRepository pointOutRepository = new PointOutRepository();
        List<PointOutOrder> pointOutOrderList = pointOutRepository.getPointOutOrderList(sqltimeTest);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(pointOutOrderList);
        response.setContentType("text/html; charset=utf-8");
        response.getWriter().write(json);

        logger.info("exchange successful");
    }
}
